package com.dongjiantong.web.boss.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.dongjiantong.sys.model.SysLabelModel;
import com.dongjiantong.sys.model.SysArea;
import com.dongjiantong.sys.service.SysAreaService;
import com.dongjiantong.user.model.RegionAccount;
import com.dongjiantong.user.service.OrgUserAreaService;
import com.dongjiantong.user.service.RegionAccountService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.shaded.com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by cao on 2017/11/10.
 */

//@RestController
//@RequestMapping("/api/boss/area")

@Slf4j
@OpenBean
public class AreaController {


    @Reference(version = "1.0.0")
    public RegionAccountService regionAccountService;

    @Reference(version = "1.0.0")
    public SysAreaService sysAreaService;


    @Reference(version = "1.0.0")
    private OrgUserAreaService orgUserAreaService;



    @OpenMethod(
            key = "boss.area.provinceList",
            httpMethods = RequestMethod.GET
    )
    public Response<List<SysLabelModel>> getProvinceList()
    {

        List<SysArea> list = sysAreaService.getProvinceList();

        SysLabelModel labelModel;
        List<SysLabelModel> labelModelList = new ArrayList<SysLabelModel>();
        for (SysArea area : list) {

            labelModel = new SysLabelModel();
            labelModel.setLabel(area.getArea_name());
            labelModel.setValue(area.getProvince_id());
            labelModel.setLevel(1);
            labelModelList.add(labelModel);
        }

        return Response.ok(labelModelList);
    }


    //@GetMapping("/cityList")
    @OpenMethod(
            key = "boss.area.cityList",
            paramNames = {"provinceId"},
            httpMethods = RequestMethod.GET
    )
    public Response<List<SysLabelModel>> getCityList(Integer provinceId)
    {
        List<SysArea> list = sysAreaService.getCityList(provinceId);
        SysLabelModel labelModel;
        List<SysLabelModel> labelModelList = new ArrayList<SysLabelModel>();
        for (SysArea area : list) {
            labelModel = new SysLabelModel();
            labelModel.setLabel(area.getArea_name());
            labelModel.setValue(area.getCity_id());
            labelModel.setLevel(2);
            labelModelList.add(labelModel);
        }

        return Response.ok(labelModelList);


    }

    //@GetMapping("/regionList")
    @OpenMethod(
            key = "boss.area.regionList",
            paramNames = {"provinceId","cityId"},
            httpMethods = RequestMethod.GET
    )
    public Response<List<SysLabelModel>> getRegionList(Integer provinceId,Integer cityId)
    {
        List<SysArea> list = sysAreaService.getRegionList(provinceId,cityId);

        SysLabelModel labelModel;
        List<SysLabelModel> labelModelList = new ArrayList<SysLabelModel>();
        for (SysArea area : list) {
            labelModel = new SysLabelModel();
            labelModel.setLabel(area.getArea_name());
            labelModel.setValue(area.getRegion_id());
            labelModel.setLevel(3);
            labelModelList.add(labelModel);
        }

        return Response.ok(labelModelList);

    }

    //@GetMapping("/streetList")
    @OpenMethod(
            key = "boss.area.streetList",
            paramNames = {"provinceId","cityId","regionId"},
            httpMethods = RequestMethod.GET
    )
    public Response<List<SysLabelModel>> getStreetList(Integer provinceId,Integer cityId,Integer regionId)
    {


        List<SysArea> list = sysAreaService.getStreetList(provinceId, cityId, regionId);
        SysLabelModel labelModel;
        List<SysLabelModel> labelModelList = new ArrayList<SysLabelModel>();
        for (SysArea area : list) {

            labelModel = new SysLabelModel();

            labelModel.setLabel(area.getArea_name());
            labelModel.setValue(area.getStreet_id());
            labelModel.setLevel(4);
            labelModelList.add(labelModel);
        }

        return Response.ok(labelModelList);


    }


    /**
     * 搜索区域公共方法
     *
     * @return
     */


    //@GetMapping("/queryRegionArea")
    @OpenMethod(
            key = "boss.area.queryRegionArea",
            paramNames = {"regionAccountId"},
            httpMethods = RequestMethod.GET
    )
    public  Response<List<Object>> getSearchRegionArea(Integer regionAccountId)
    {

        //如果regionAccountId为0,表示是boss查询所的已经开户的区域

        if (regionAccountId == null) {
            throw new OPServerException(HttpCode.BAD_REQUEST, "参数不能为空。");
        }

        return Response.ok(treeNode(regionAccountId));

    }

    private List<Object> treeNode(long pid) {
        List<RegionAccount> childTreeNodes = regionAccountService.queryTreeNode(pid);

        List<Object> node = Lists.newArrayList();

        if (childTreeNodes.size() > 0) {
            for (RegionAccount ra : childTreeNodes) {

                Map<String, Object> childArray = Maps.newHashMap();

                if (ra.getParentId() == pid) {
                    childArray.put("label", ra.getAreaName());
                    childArray.put("value", ra.getAreaCode());

                    if (ra.getLevel() == 4) {
                        childArray.put("children", queryVillage(ra.getProvinceId(), ra.getCityId(), ra.getRegionId(), ra.getStreetId()));
                    } else {
                        childArray.put("children", treeNode(ra.getId()));
                    }
                    node.add(childArray);

                }

            }

        }
        return node;
    }

    private List<Object> queryVillage(Integer province_id, Integer city_id, Integer region_id, Integer street_id) {
        List<SysArea> list = sysAreaService.getVillageList(province_id, city_id, region_id, street_id);

        List<Object> node = Lists.newArrayList();

        if (list.size() > 0) {
            for (SysArea ra : list) {

                Map<String, Object> childArray = Maps.newHashMap();
                childArray.put("label", ra.getArea_name());
                childArray.put("value", ra.getVillage_id());
                node.add(childArray);
            }
        }
        return node;
    }



}
